<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>角色管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../css/public.css" media="all">
</head>
<body>

<div class="layui-row">
    <fieldset class="table-search-fieldset">
        <legend>搜索信息</legend>
        <div style="margin: 10px 10px 10px 10px">
            <form class="layui-form layui-form-pane">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">角色名称</label>
                        <div class="layui-input-inline">
                            <input type="text" name="roleName" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">角色编号</label>
                        <div class="layui-input-inline">
                            <input type="text" name="roleCode" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                    </div>
                </div>
            </form>
        </div>
    </fieldset>
</div>
<div class="layui-row">
    <div class="layui-col-md8">
        <div class="layuimini-container">
            <div class="layuimini-main">
                <script type="text/html" id="RoleToolBar">
                    <div class="layui-btn-container">
                        <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="rbac:role:add"> 添加 </button>
                        <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="rbac:role:batchdel"> 删除 </button>
                        <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="rbac:role:refresh"> 刷新缓存 </button>
                    </div>
                </script>

                <table class="layui-hide" id="RoleTableId" lay-filter="RoleTableFilter"></table>

                <script type="text/html" id="RoleTableBar">
                    <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="rbac:role:edit">修改</a>
                    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="rbac:role:del">删除</a>
                    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-edit" lay-event="rbac:role:setmenu">权限</a>
                </script>
            </div>
        </div>
    </div>
    <div class="layui-col-md3" style="margin-left: 10px;">
        <div class="layuimini-container">
            <div class="layuimini-main">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="rbac:role:saveMenu" lay-click="saveMenu"> 保存 </button>
                </div>
                <div style="margin-top: 5px;">
                    <span id="currentRoleInfo">请选择要维护的角色</span>
                    <input type="hidden" name="currentRoleId" id="currentRoleId">
                    <hr class="layui-bg-orange">
                </div>
                <div id="menuTree" class="demo-tree-more"></div>
            </div>
        </div>
    </div>
</div>
<script src="../../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../../js/lay-config.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'tree', 'util'], function () {
        var $ = layui.jquery,
            form = layui.form,
            tree = layui.tree,
            util = layui.util,
            table = layui.table;

        function getTreeData(){
            $.ajax({
                type: 'GET',
                url: 'admin/rbac/menu/tree/'+$("#currentRoleId").val(),
                contentType: 'application/json',
                success:function(result){
                    if(result.code==0){
                        tree.render({
                            elem: '#menuTree',
                            data: result.data,
                            showCheckbox: true,  //是否显示复选框
                            id: 'role-menu-tree',
                            isJump: true
                        });
                    }else{
                        layer.alert(result.msg);
                    }
                }
            });
        }

        //按钮事件
        util.event('lay-click', {
            saveMenu: function(othis){
                if($("#currentRoleId").val()==""){
                    layer.alert("请选择您要操作的角色");
                    return;
                }
                var data = tree.getChecked('role-menu-tree'); //获取选中节点的数据
                var choosedNodes=[];
                handleData(data, choosedNodes, $("#currentRoleId").val());
                if(choosedNodes.length<1){
                    let obj={};
                    obj.roleId = $("#currentRoleId").val();
                    choosedNodes.push(obj);
                }
                $.ajax({
                    type: 'POST',
                    url: 'admin/rbac/rolemenu',
                    data: JSON.stringify(choosedNodes),
                    contentType: 'application/json',
                    success:function(result){
                        layer.alert(result.msg);
                    }
                });
            }
        });

        //递归获取选中的节点
        function handleData(data, result, roleId){
            if(data.children<=0){
                return false;
            }
            data.forEach(function(item,i){
                let obj={};
                obj.menuId=item.id;
                obj.roleId = roleId;
                result.push(obj);
                if(item.children && item.children.length>0){
                    handleData(item.children, result, roleId);
                }
            });
            return result;
        }

        table.render({
            elem: '#RoleTableId',
            url: 'admin/rbac/role/page',
            method: 'GET',
            toolbar: '#RoleToolBar',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            request: {
                pageName: 'current', //页码的参数名称，默认：page
                limitName: 'size' //每页数据量的参数名，默认：limit
            },
            parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.records //解析数据列表
                };
            },
            cols: [[
                {type: 'checkbox', width: 80},
                {field: 'pkId', width: 100, align: "center", title: '主键'},
                {field: 'roleName', minWidth: 200, title: '角色名称'},
                {field: 'roleCode', minWidth: 200, title: '角色编号'},
                {field: 'dataScope', width: 150, title: '数据范围', align: "center", templet:function(col)
                    {
                        if (col.dataScope == 1) {
                            return '<span class="layui-badge layui-bg-blue">全部</span>';
                        } else if (col.dataScope == 2) {
                            return '<span class="layui-badge layui-bg-blue">单位</span>';
                        } else if (col.dataScope == 3) {
                            return '<span class="layui-badge layui-bg-blue">部门</span>';
                        } else if (col.dataScope == 4) {
                            return '<span class="layui-badge layui-bg-blue">个人</span>';
                        }
                    }
                },
                {field: 'roleStatus', width: 150, title: '状态', align: "center", templet:function(col)
                    {
                        return col.roleStatus == 1 ? '<span class="layui-badge layui-bg-blue">可用</span>' : '<span class="layui-badge-rim">禁用</span>';
                    }
                },
                {title: '操作', minWidth: 150, toolbar: '#RoleTableBar', align: "center", fixed: 'right'}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            height: 'full-148'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            //执行搜索重载
            table.reload('RoleTableId', {
                page: {
                    curr: 1
                },
                where: {
                    searchParams: result
                }
            }, 'data');
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(RoleTableFilter)', function (obj) {
            if (obj.event === 'rbac:role:add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加角色',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['40%', '70%'],
                    content: 'add.html'
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = main.checkStatus('RoleTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            } else if (obj.event === 'rbac:role:refresh') {
                layer.confirm('确定要重置缓存数据?', {icon: 3, title:'提示'}, function(index){

                    $.ajax({
                        type: 'POST',
                        url: 'admin/rbac/role/refreshCache',
                        contentType: 'application/json',
                        success:function(result){
                            layer.alert(result.msg);
                        }
                    });
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(RoleTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(RoleTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'rbac:role:edit') {
                layer.open({
                    title: '修改权限',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    area: ['40%', '70%'],
                    content: 'edit.html',
                    success: function(layero, index){
                        var iframe = window['layui-layer-iframe' + index];
                        iframe.setValue(data.pkId);
                    }
                });
            } else if (obj.event === 'rbac:role:del') {
                layer.confirm('确定要删除?', {icon: 3, title:'提示'}, function(index){

                    $.ajax({
                        type: 'DELETE',
                        url: 'admin/rbac/role/'+data.pkId,
                        contentType: 'application/json',
                        success:function(result){
                            if(result.code==0){
                                layer.alert("删除成功");
                                table.reload('RoleTableId');
                            }else{
                                layer.alert(result.msg);
                            }
                        }
                    });
                });
            } else if (obj.event === 'rbac:role:setmenu') {
                if(data.pkId!=$("#currentRoleId").val()){
                    $("#currentRoleId").val(data.pkId);
                    $("#currentRoleInfo").html('当前选中： '+data.roleName+' - '+data.roleCode);
                    getTreeData();
                }
            }
        });
        $("#currentRoleId").val("");
    });
</script>

</body>
</html>